// pages/myCenter/myCenter.js
import { Home } from '../home/home-model.js';
import { Config } from '../../utils/config.js';
var home = new Home(); //实例化 首页 对象


Page({

  /**
   * 页面的初始数据
   */
  data: {
    iconSrc:'../../images/myCenter/head.jpeg',
    name:'雷电',
    balance:1920,
    calculating:1920,
    articleNum:0,
    background:'',
    resNum:0,
    status:0,
    cash:0,
    error:'提现失败',
    hidden:true,
    extraList:[
      { iconSrc: Config.baseUrl+'/Public/Api/images/r2.png', text: '我的收藏'},
      { iconSrc: Config.baseUrl + '/Public/Api/images/r3.png', text: '在线客服'},
      { iconSrc: Config.baseUrl + '/Public/Api/images/r4.png',text:'关于我们'}
    ],
    right: Config.baseUrl + '/Public/Api/images/w2.png',
    cash_btn:'',
    cash_submit:''
  },
  btnTap:function(e){
    console.log(e.target.dataset.text);
    if (e.target.dataset.text=='我的收藏'){
      wx.navigateTo({
        url: '../../pages/collection/collection',
      })
    }
  },
  openCashMask:function(){
    this.setData({
      hidden: false
    });
  },
  close:function(){
    this.setData({
      hidden: true
    });
  },
  change:function(e){
    this.setData({
      cash: parseInt(e.detail.value)
    });
  },
  getAll:function(){
    this.setData({
      cash: this.data.balance
    });
    this.getCash();
  },
  //提现
  getCash:function(){
    var session_key = wx.getStorageSync('session_key');
    if (session_key == '') {
      wx.showToast({
        title: '没有登录',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (parseInt(this.data.cash)<1){
      wx.showToast({
        title: '提现金额最小1.00元',
        icon: 'none',
        duration: 2000
      })
      return false;
    }

    if (parseInt(this.data.cash)>=20000) {
      wx.showToast({
        title: '提现金额不能超过20000元',
        icon: 'none',
        duration: 2000
      })
      return false;
    }

    var data = {
      "session_key": session_key,
      "money": parseInt(this.data.cash),
    }
    home.postCash(data, (res) => {
      if (res.status) {
        this.setData({
          status: 1
        });
      } else {
        this.setData({
          status: -1,
          error: res.msg
        });
      }
      setTimeout(function () {
        this.setData({
          hidden: true,
          status: 0,
          cash: 0,
        });
      }.bind(this), 3000);

    });



  },
  goArticle:function(e){
    console.log(e.target.id);
    if(e.target.id!=''){
      wx.navigateTo({
        url: '../../pages/article/article?type=' + e.target.id
      });
    }
  },
  goSystemMsg:function(){
    wx.navigateTo({
      url: '../../pages/systemMsg/systemMsg'
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var session_key = wx.getStorageSync('session_key');
    var that = this;
    // 获得信息
    home.getMyInfo(session_key, (data) => {
      if (data.status) {
        var info = data.info;
        var bgimg = data.bgimg;
        var background = Config.baseUrl + bgimg.background;
        var cash_btn = Config.baseUrl + bgimg.cash_btn;
        var cash_submit = Config.baseUrl + bgimg.cash_submit;
        that.setData({
          name: info.nickname,
          balance: info.balance,
          articleNum: info.articleNum,
          resNum: info.resNum,
          iconSrc: info.avatar,
          background: background,
          cash_btn:cash_btn,
          cash_submit: cash_submit
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})